/**
 * zmn文章模块-文章列表
 * 文件命名格式：zmn.模块名称.表名称.功能.jas
 * @author heguanghua
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;
    var laydate = layui.laydate;

    var data = { "code":0, "msg":"", "count":json.totalCount, "items":json.items };
    var cols = [
        [
            { field:'newsId', type:'checkbox', fixed:'left', width:50, align:'center' },
            { field:'sort', title:'排序', width:70, align:'center', edit:'text', unresize:true, sort:false },
            { field:'newsId', title:'ID', width:70, align:'center', unresize:true, sort:false },
            { field:'title', title:'标题', templet:function(d) {
                return (json.hasView ? zmnTpl.linkTpl(d.title, '/news/detail.action?newsId=', d.newsId) : '{{d.title}}');
            }},
            { field:'categName', title:'所属分类', width:120, align:'center' },
            { field:'sourceType', title:'来源类型', width:100, align:'center', unresize:true, templet: function(d) {
                return $("select[name='sourceType'] option[value='" + d.sourceType + "']").text();
            }},
            { field:'updateTime', title:'编辑人/时间', width:200, align:'center', unresize:true},
            { field:'status', title:'状态', width:80, align:'center', unresize:true, templet: function(d) {
                return zmnTpl.statusTpl(d.status);
            }},
            { fixed:'right', title:'操作', width:140, align:'center', templet:function(d) {
                return (json.hasView ? zmnTpl.linkBtnTpl('编辑', d.title, '/news/detail.action?newsId=', d.newsId) : '') + (json.hasContent ? zmnTpl.linkBtnTpl('内容' ,d.title, '/news/content.action?newsId=', d.newsId) : '');
            }}
        ]
    ];
    table.render({
        elem:    "#zmn-news-table",
        toolbar: "#zmn-news-table-tool",
        title:   "文章列表",
        page: { count: data.count },
        data: data.items,
        cols: cols,
        height: 'full-214'
    });

    //日期
    laydate.render({ elem: "#updateTimeBegin"});
    laydate.render({ elem: "#updateTimeEnd"});

    // 分类下拉框联动
    zmnPage.laySelectLinkage(form, "/common/news/category/listChilds.action", "categOneId", "categTwoId", "categId");

    var ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-news-table)",  function (obj) {
        ids = zmnPage.layIdsChecked(data.items, "newsId");
    });

    // 启用/停用
    table.on("toolbar(zmn-news-table)", function (obj) {
        if(obj.event != "start" && obj.event != "stop") { return; }
        if(ids.length === 0){ return layer.alert("请选择至少一篇要操作的文章！"); }
        zmnPage.layStartStop(obj, "/news/updateStatus.action", ids.join(","));
    });

    // 修改排序
    table.on("edit(zmn-news-table)", function(obj){
        zmnPage.laySort(obj, "/news/updateSort.action", obj.data.newsId);
    });

    //对外暴露的接口
    exports('product_list', {});
});